const express = require('express');
const bodyParser = require('body-parser');
const mysql = require('mysql');
const path = require("path");
const app = express();
app.use(bodyParser.json());
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'wzbWZB123',
database: 'login_exam'
});
app.get("/", (req, res) => {
    res.sendFile(path.join(__dirname, "views", "login.html")); 
});
// 设置静态文件目录
app.use(express.static(path.join(__dirname, 'views')));
app.post('/login', (req, res) => {
const { username, password } = req.body;
db.query('SELECT * FROM users WHERE username = ? AND password = ?', [username,
    password], (err, results) => {
    if (err) {
    return res.status(500).json({ success: false, message: '服务器错误' });
    }
    if (results.length > 0) {
    res.json({ success: true });
    } else {
    res.json({ success: false, message: '用户名或者密码不正确' });
    }
    });
    });
    app.listen(3000, () => {
    console.log('Server running on http://localhost:3000');
    });